export const policyPreview = (info) => {
	let openFlag = 0;
	switch (handlesuffix(info.org_name)) {
		case 1: //文件
			openFlag = previewfile(info.pdf_url ? info.pdf_url : info.path_url)
			break;
		case 2: // 图片
			openFlag = previewImage(info.path_url)
			break;
	}
	return openFlag;
}
export const handlesuffix = (name) => {
	const nameSplit = name.split('.');
	const suffix = nameSplit[nameSplit.length - 1]
	const fileSuffix = ['txt', 'pdf', 'doc', 'docx', 'xls', 'xlsx']
	const imageSuffix = ['png', 'jpg', 'bmp', 'bmp']
	if (fileSuffix.some(item => {
			return item == suffix
		})) {
		return 1
	}
	if (imageSuffix.some(item => {
			return item == suffix
		})) {
		return 2
	}
}
var isopen = true;
export const previewfile = (url) => {
	if (!isopen) {
		return;
	}
	isopen = false;
	var openRes = 0;
	uni.downloadFile({
		url: url,
		success: function(res) {
			var filePath = res.tempFilePath;
			uni.openDocument({
				filePath: filePath,
				showMenu: true,
				success: function(res) {
					console.log('打开文档成功');
					isopen = true;
					openRes = 1;
				}
			});
		},
		fail: (err) => {
			console.error('下载错误', err);
			isopen = true;
			openRes = 0;
		}
	});
	return openRes;
}
// 多张 图片预览
export const previewImage = (url) => {
	uni.previewImage({
		current: 1, // 当前显示图片的索引值
		urls: [url], // 需要预览的图片列表，photoList要求必须是数组
		loop: true, // 是否可循环预览
	})
}
export const previewVideo = (url) => {
	uni.downloadFile({
		url: url, //仅为示例，并非真实的资源
		success: (res) => {
			if (res.statusCode === 200) {
				uni.saveVideoToPhotosAlbum({
					filePath: res.tempFilePath,
					success: function() {
						uni.showToast({
							title: '视频下载成功，请在相册查看',
							icon: 'none'
						})
					},
					fail: function(err) {
						console.log(err, 55)
						uni.showToast({
							title: '视频下载失败',
							icon: 'none'
						})
					}
				});
			} else {
				uni.showToast({
					title: '视频下载失败',
					icon: 'none'
				})
			}
		},
		fail: () => {
			uni.showToast({
				title: '视频下载失败',
				icon: 'none'
			})
		}
	});
}